import os
import sys
import config

Execute(Mkdir(config.OUT_DIR))
Execute(Mkdir(config.BIN_DIR))
Execute(Mkdir(config.RELEASE_DIR))
Execute(Mkdir(config.PUBLIC_DIR))

ROOT = os.getcwd()
Export('ROOT')

#// include ==============================================
INCLUDES = []
list = os.listdir(ROOT)
for dir in list:
    path = os.path.join(ROOT, dir)
    if os.path.isfile(os.path.join(path, 'include.py')):
        INCLUDES += Split(path)
        INCLUDES += SConscript(os.path.join(dir, 'include.py'))
#print('path:', INCLUDES)


#// Environment ==========================================
env = Environment(ENV  = os.environ,      
		CPPPATH= INCLUDES,	
		CFLAGS = config.CFLAGS,
	    CC     = config.CC, 		CCFLAGS   = config.CCFLAGS,
	    CXX    = config.CXX, 		CXXFLAGS  = config.CXXFLAGS,
	    AS     = config.AS, 		ASFLAGS   = config.ASFLAGS,
	    AR     = config.AR, 		ARFLAGS   = config.ARFLAGS,
	    LINK   = config.LINK,		LINKFLAGS = config.LDFLAGS,
	    STRIP   = config.STRIP)
#env.AppendENVPath('LIBPATH', config.LIBPATH)
env.Append(LIBPATH = config.LIBPATH)
env.Replace(LIBS=  config.LIBS)
env['LINKCOM']    = '$LINK -o $TARGET $LINKFLAGS $__RPATH $_LIBDIRFLAGS -Wl,"-(" $SOURCES $_LIBFLAGS -Wl,"-)" '
env['CCCOMSTR']   = '$CC  $SOURCES'
env['CXXCOMSTR']  = '$CXX  $SOURCES'
env['ARCOMSTR']   = '$AR -o $TARGET'
env['LINKCOMSTR'] = '$LINK -o $TARGET'
Export('env')
#for item in env.Dictionary():
#	print("--(%s:%s)" %(item, env[item]))

OBJS = []
list = os.listdir(ROOT)
for dir in list:
    path = os.path.join(ROOT, dir)
    if os.path.isfile(os.path.join(path, 'makefile.py')):
        OBJS += SConscript(os.path.join(dir, 'makefile.py'))
#print('OBJS:', OBJS)


#// output file ==========================================


#// 1. output  share lib
# sharedLib = env.SharedLibrary(target='example', source= OBJS)
# env.Install(config.RELEASE_DIR,  sharedLib)


#// 2. output  static lib
# staticLib = env.StaticLibrary(target='example',source= OBJS)
# env.Install(config.RELEASE_DIR,  staticLib)


#// 3. output  execfile
env.Program(config.ELF_FILE,  OBJS)
env.Command(target=config.ELF_STRIP, source=config.ELF_FILE,
            action=config.STRIP + ' -o $TARGET $SOURCE')
# env.Install(config.RELEASE_DIR,  config.ELF_STRIP)

